//
//  UIButton+CZAddition.m
//  Weibo_Swift
//
//  Created by MT on 2017/5/9.
//  Copyright © 2017年 weibo. All rights reserved.
//

#import "UIButton+CZAddition.h"

@implementation UIButton (CZAddition)
+ (instancetype)cz_textButton:(NSString *)title fontSize:(CGFloat)fontSize normalColor:(UIColor *)normalColor selectedColor:(UIColor *)selectedColor backgroundImageName:(NSString *)backgroundImageName{
    
    UIButton *button = [[UIButton alloc] init];
    //    button.highlighted = NO;
    //    button.showsTouchWhenHighlighted = YES;
    
    [button setTitle:title forState:UIControlStateNormal];
    
    [button setTitleColor:normalColor forState:UIControlStateNormal];
    [button setTitleColor:selectedColor forState:UIControlStateHighlighted];
    
    button.titleLabel.font = [UIFont systemFontOfSize:fontSize];
    [button setBackgroundImage:[UIImage imageNamed:backgroundImageName] forState:UIControlStateNormal];
    [button sizeToFit];
    
    return button;
}

-(void)setButtonImageWithUrl:(NSString *)urlStr {
    NSURL * url = [NSURL URLWithString:urlStr];
    // 根据图片的url下载图片数据
    dispatch_queue_t xrQueue = dispatch_queue_create("loadImage", NULL); // 创建GCD线程队列
    dispatch_async(xrQueue, ^{
        
        // 异步下载图片
        UIImage * img = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
        // 主线程刷新UI
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self setImage:[img imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        });
    });
}



@end
